package com.soft.bams.dao;

/**
 * @author yangwenqi
 * @decs TODO
 * @date 2025/4/18
 */

import com.soft.bams.entity.Account;
import java.util.Arrays;
//要求2：完成编码。
//ArrayDao类  dao 数据交互层
//属性:
//        1.当前所有的账户对象的信息,存放在数组中: Account[].
//        2.当前账户数量index.
//insert(Account acc)
//selectOne(Long id,String pwd)
//selectOne(Long id)
//selectAll()
//delete(Long id)         ？？？ 慎写
//update(Account acc)  ？？？ 基于内存来操作的，数组、集合的存储方式，需要更新操作么？
public  class ArrayDao implements IDao<Account,Account[]>{
    private Account[] accounts = new Account[10];
    private int index = 0;
    private Long id=100000000000000000L;

    @Override
    public Long getId() {
        return this.id;
    }

    @Override
    public void setId(Long id) {
        this.id = id;
    }

    //插入新用户
    public void insert(Account acc) {
        if (index >= accounts.length) {
//            Account[] newAccounts = new Account[accounts.length * 3/2+1];
//            System.arraycopy(accounts, 0, newAccounts, 0, accounts.length);
//            accounts = newAccounts;
            accounts = Arrays.copyOf(accounts , accounts .length*3/2+1);

        }
        accounts[index++] = acc;
    }
    //根据ID和密码查询账户
    public Account selectOne(Long id, String pwd) {
        for (Account a : accounts) {
            if (a != null && a.getId().equals(id) && a.getPassword().equals(pwd)) {
                return a;
            }
        }
        return null;
    }
    //根据ID查询数据
    public Account selectOne(Long id) {
        for (Account a : accounts) {
            if (a != null && a.getId().equals(id)) {
                return a;
            }
        }
        return null;
    }
    public Account[] selectAll() {
        return accounts;
    }
}




